package hotel.meituan.oversea.biz.controller;

import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.meituan.request.goods.MtHotelGoodsPriceParamVo;
import hotel.base.oversea.vo.meituan.request.goods.MtHotelOneGoodsPriceParamVo;
import hotel.meituan.oversea.biz.config.service.RedisService;
import hotel.meituan.oversea.biz.service.MtHotelGoodsPriceService;
import hotel.meituan.oversea.biz.service.MtHotelGoodsService;
import hotel.meituan.oversea.biz.task.MeituanSaveUpdateGoodsPriceTableTask;
import hotel.meituan.oversea.biz.task.MeituanSaveUpdateGoodsStatusTableTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 美团酒店产品价格Controller
 * @author qiuyy
 * @date 2020-05-15
 */
@Slf4j
@RestController
@RequestMapping("/mtHotelGoodsPrice")
@Api(value = "MtHotelGoodsPriceController", tags = {"美团酒店产品价格接口"})
public class MtHotelGoodsPriceController {
    @Autowired
    private MtHotelGoodsService mtHotelGoodsService;
    @Autowired
    private MtHotelGoodsPriceService mtHotelGoodsPriceService;
    @Autowired
    private MeituanSaveUpdateGoodsPriceTableTask meituanSaveUpdateGoodsPriceTableTask;
    @Autowired
    private MeituanSaveUpdateGoodsStatusTableTask meituanSaveUpdateGoodsStatusTableTask;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/getMtHotelGoodsPrice")
    @ApiOperation(value = "查询产品价格日历",notes="必须经过auth授权")
    public Result<Object> saveGetMtHotelGoodsPrice(@RequestBody MtHotelGoodsPriceParamVo mtHotelGoodsPriceParamVo){
        //上游要求：业务参数，hotelId和goodsId传其一，最好hotelId，个数支持最多10
        //我们这边最好让下游都hotelId和goodsId都上送，，，上游有返回entity对应hotelId

        List<Long> hotelIdList = mtHotelGoodsPriceParamVo.getHotelIds();
        List<Long> goodsIdList = mtHotelGoodsPriceParamVo.getGoodsIds();
        if(null == hotelIdList || hotelIdList.size()==0){
            if(null == goodsIdList || goodsIdList.size()==0){
                return new Result<>("hotelIds或goodsIds不可为空",CommonConstants.REQUEST_ERROR);
            }
        }else{
            if(null == goodsIdList || goodsIdList.size()==0){
                if(null == hotelIdList || hotelIdList.size()==0){
                    return new Result<>("hotelIds或goodsIds不可为空",CommonConstants.REQUEST_ERROR);
                }
            }
        }
        String startDate = mtHotelGoodsPriceParamVo.getStartDate();
        String endDate = mtHotelGoodsPriceParamVo.getEndDate();
        //时间参数为空 默认30天
        if(StringUtils.isEmpty(startDate)){
            startDate = DateUtil.formatDate(new Date(),"yyyy-MM-dd");
            endDate = DateUtil.formatDate(DateUtil.addDay(new Date(),30),"yyyy-MM-dd");
        }
        return mtHotelGoodsService.saveGetMtHotelGoodsPrice(hotelIdList,goodsIdList,startDate,endDate,null);
    }

//    @PostMapping("/getMtHotelGoodsPriceForFp")
//    @ApiOperation(value = "查询产品价格日历给飞猪",notes="必须经过auth授权")
//    public Result<Object> getMtHotelGoodsPriceForFp(@RequestBody CallbackChangeGoodsVo callbackChangeGoodsVo){
//        if(null != callbackChangeGoodsVo && callbackChangeGoodsVo.getHotelsGoodsData().size()>0){
//            String startDate = callbackChangeGoodsVo.getStartDate();
//            String endDate = callbackChangeGoodsVo.getEndDate();
//            List<Long> hotelIdList  = new ArrayList<>();
//            Map allGoodsMap = new HashMap();
//            for (CallbackChangeGoodsDetailVo oneHotel:callbackChangeGoodsVo.getHotelsGoodsData()){
//                String thisHotelId = oneHotel.getHotelId();
//                List<String> goodsIdList = oneHotel.getGoodsIdList();
//                if(null != goodsIdList && goodsIdList.size()>0){
//                    for (String gid:goodsIdList){
//                        allGoodsMap.put(thisHotelId+"_"+gid,1);
//                    }
//                }
//                hotelIdList.add(Long.valueOf(thisHotelId));
//            }
//
//            if(null != hotelIdList && hotelIdList.size()>0){
//               /* String startDate = DateUtil.formatDate(new Date(),"yyyy-MM-dd");
//                String endDate = DateUtil.formatDate(DateUtil.addDay(startDate,30),"yyyy-MM-dd");*/
//                Result rs = mtHotelGoodsService.saveGetMtHotelGoodsPrice(hotelIdList,null,startDate,endDate,null);
//                if(rs.getCode()==CommonConstants.SUCCESS){
//                    List<MtHotelGoodsPriceBaseVo> mtHotelGoodsPriceBaseVoListNew = new ArrayList<>();
//                    List<MtHotelGoodsPriceBaseVo> mtHotelGoodsPriceBaseVoList = (List<MtHotelGoodsPriceBaseVo>)rs.getData();
//                    if(null != mtHotelGoodsPriceBaseVoList && mtHotelGoodsPriceBaseVoList.size()>0){
//                        for (MtHotelGoodsPriceBaseVo oneGoods:mtHotelGoodsPriceBaseVoList){
//                            String gid = String.valueOf(oneGoods.getGoodsId());
//                            String hotelId = String.valueOf(oneGoods.getHotelId());
//                            if(null != allGoodsMap.get(hotelId+"_"+gid)){
//                                mtHotelGoodsPriceBaseVoListNew.add(oneGoods);
//                            }
//                        }
//                        if(null != mtHotelGoodsPriceBaseVoListNew && mtHotelGoodsPriceBaseVoListNew.size()>0){
//                            return new Result<>(mtHotelGoodsPriceBaseVoListNew);
//                        }
//                    }
//                }
//            }
//        }
//        return new Result<>();
//    }

    /**
     * 内部外放接口
     * @return
     */
    @PostMapping("/getMtHotelOneGoodsPrice")
    @ApiOperation(value = "查询产品价格日历根据商品id",notes="必须经过auth授权")
    public Result<Object> getMtHotelOneGoodsPrice(@RequestBody MtHotelOneGoodsPriceParamVo mtHotelOneGoodsPriceParamVo){
        //log.info("请求产品价格日历的数据为{}",mtHotelOneGoodsPriceParamVo);
        Long goodsId = mtHotelOneGoodsPriceParamVo.getGoodsId();
        if(null == goodsId){
            return new Result<>("goodsId不可为空",CommonConstants.REQUEST_ERROR);
        }
        Integer days = mtHotelOneGoodsPriceParamVo.getDays();
        String startDate = null;
        String endDate = null;
        if(null != days){
            startDate = DateUtil.formatDate(new Date(),"yyyy-MM-dd");
            endDate = DateUtil.formatDate(DateUtil.addDay(new Date(),days),"yyyy-MM-dd");
        }else{
            startDate = mtHotelOneGoodsPriceParamVo.getStartDate();
            endDate = mtHotelOneGoodsPriceParamVo.getEndDate();
            if(StringUtils.isEmpty(startDate)){
                return new Result<>("startDate不可为空",CommonConstants.REQUEST_ERROR);
            }
            if(StringUtils.isEmpty(endDate)){
                return new Result<>("endDate不可为空",CommonConstants.REQUEST_ERROR);
            }
        }

        return mtHotelGoodsService.saveGetMtHotelGoodsPrice(goodsId,startDate,endDate);
    }

    @PostMapping("/pullTodayGoodsPriceAboutCompareHotel")
    @ApiOperation(value = "拉取今日产品价格关于所有对比后状态为有效的酒店",notes="必须经过auth授权")
    public Result<Object> pullTodayGoodsPriceAboutCompareHotel(@RequestBody Map map){
        log.info("pullTodayGoodsPriceAboutCompareHotel  拉取今日产品价格关于所有对比后状态为有效的酒店  开始时间： {},map={}", DateUtil.getCurrTime(), JSON.toJSONString(map));
        Integer maxPrice = (Integer) map.get("maxPrice");
        Result result = mtHotelGoodsService.pullTodayGoodsPriceAboutCompareHotel(maxPrice);
        log.info("pullTodayGoodsPriceAboutCompareHotel  拉取今日产品价格关于所有对比后状态为有效的酒店  结束时间： {}", DateUtil.getCurrTime());
        return result;
    }

}
